{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Snippets and Programs from Chapter 1: Numbers\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(3, 4)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# P5: fractions demo\n",
    ">>> from fractions import Fraction\n",
    ">>> f = Fraction(3, 4)\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a complex number: 2+3j\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(2+3j)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#P12: Complex number input\n",
    ">>> z = complex(input('Enter a complex number: ')) #2+3j is valid, 2 + 3j is invalid\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#P12: Checking if a number is a factor of another\n",
    "def is_factor(a, b):\n",
    "    if b % a == 0:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "# try it out\n",
    "print(is_factor(4, 1024))\n",
    "print(is_factor(4, 21))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your Number Please: 25\n",
      "1\n",
      "5\n",
      "25\n"
     ]
    }
   ],
   "source": [
    "#P14: Find the factors of an integer\n",
    "'''\n",
    "Find the factors of an integer\n",
    "'''\n",
    "def factors(a):\n",
    "    for i in range(1, a+1):\n",
    "        if a % i == 0:\n",
    "            print(i)\n",
    "            \n",
    "if __name__ == '__main__':\n",
    "    a = input('Your Number Please: ')\n",
    "    a = float(a)\n",
    "    if a > 0 and a.is_integer():\n",
    "        factors(int(a))\n",
    "    else:\n",
    "        print('Please enter a positive integer')\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "At the grocery store, I bought some apples and bananas and grapes\n"
     ]
    }
   ],
   "source": [
    "#P15: Format example\n",
    "\n",
    ">>> item1 = 'apples'\n",
    ">>> item2 = 'bananas'\n",
    ">>> item3 = 'grapes'\n",
    ">>> print('At the grocery store, I bought some {0} and {1} and {2}'.format(item1, item2, item3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number: 5\n",
      "5.0 x 1 = 5.0\n",
      "5.0 x 2 = 10.0\n",
      "5.0 x 3 = 15.0\n",
      "5.0 x 4 = 20.0\n",
      "5.0 x 5 = 25.0\n",
      "5.0 x 6 = 30.0\n",
      "5.0 x 7 = 35.0\n",
      "5.0 x 8 = 40.0\n",
      "5.0 x 9 = 45.0\n",
      "5.0 x 10 = 50.0\n"
     ]
    }
   ],
   "source": [
    "#P16: Multiplication table printer\n",
    "\n",
    "'''\n",
    "Multiplication table printer\n",
    "'''\n",
    "def multi_table(a):\n",
    "    for i in range(1, 11):\n",
    "        print('{0} x {1} = {2}'.format(a, i, a*i))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    a = input('Enter a number: ')\n",
    "    multi_table(float(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "37.0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#P18: Fahrenheit to Celsius conversion\n",
    ">>> F = 98.6\n",
    ">>> (F - 32) * (5 / 9)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "98.60000000000001"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#P18: Celsius to Fahrenheit\n",
    ">>> C = 37\n",
    ">>> C * (9 / 5) + 32\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. Kilometers to Miles\n",
      "2. Miles to Kilometers\n",
      "Which conversion would you like to do?: 2\n",
      "Enter distance in miles: 100\n",
      "Distance in kilometers: 160.9\n"
     ]
    }
   ],
   "source": [
    "#P19: Unit conversion\n",
    "\n",
    "'''\n",
    "Unit converter: Miles and Kilometers\n",
    "'''\n",
    "def print_menu():\n",
    "    print('1. Kilometers to Miles')\n",
    "    print('2. Miles to Kilometers')\n",
    "    \n",
    "def km_miles():\n",
    "    km = float(input('Enter distance in kilometers: '))\n",
    "    miles = km / 1.609\n",
    "    print('Distance in miles: {0}'.format(miles))\n",
    "\n",
    "def miles_km():\n",
    "    miles = float(input('Enter distance in miles: '))\n",
    "    km = miles * 1.609\n",
    "    print('Distance in kilometers: {0}'.format(km))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    print_menu()\n",
    "    choice = input('Which conversion would you like to do?: ')\n",
    "    if choice == '1':\n",
    "        km_miles()\n",
    "    if choice == '2':\n",
    "        miles_km()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.0\n",
      "-1.0\n"
     ]
    }
   ],
   "source": [
    "#P21: Roots of a quadratic equation example\n",
    ">>> a = 1\n",
    ">>> b = 2\n",
    ">>> c = 1\n",
    ">>> D = (b**2 - 4*a*c)**0.5\n",
    ">>> x_1 = (-b + D)/(2*a)\n",
    ">>> print(x_1)\n",
    ">>> x_2 = (-b - D)/(2*a)\n",
    ">>> print(x_2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a: 1\n",
      "Enter b: 2\n",
      "Enter c: 1\n",
      "x1: -1.0\n",
      "x2: -1.0\n"
     ]
    }
   ],
   "source": [
    "#P21: Quadratic Equation Root calculator\n",
    "'''\n",
    "Quadratic Equation root calculator\n",
    "'''\n",
    "def roots(a, b, c):\n",
    "    D = (b*b - 4*a*c)**0.5\n",
    "    x_1 = (-b + D)/(2*a)\n",
    "    x_2 = (-b - D)/(2*a)\n",
    "    print('x1: {0}'.format(x_1))\n",
    "    print('x2: {0}'.format(x_2))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    a = input('Enter a: ')\n",
    "    b = input('Enter b: ')\n",
    "    c = input('Enter c: ')\n",
    "    roots(float(a), float(b), float(c))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
